#ifndef PATIENTDETAILWIDGET_H
#define PATIENTDETAILWIDGET_H

#include <QWidget>
#include <QJsonArray>
#include <QJsonObject>
#include "../../../Common/models.h"
#include "../../../Common/protocol.h"

namespace Ui {
class PatientDetailWidget;
}

class PatientDetailWidget : public QWidget {
    Q_OBJECT
    
public:
    // 构造函数：传入医生信息、患者ID、预约ID
    explicit PatientDetailWidget(const Doctor& doctor, int patientId, int appointmentId, QWidget *parent = nullptr);
    ~PatientDetailWidget();
    
signals:
    void backToAppointments();  // 返回预约列表的信号
    void showCreateRecord(const Doctor& doctor, const Patient& patient, int appointmentId, const QDateTime& appointmentTime);  // 显示创建病历页面（带预约ID和预约时间）
    void showCreatePrescription(const Doctor& doctor, const Patient& patient, int appointmentId, int latestRecordId);  // 显示创建处方页面（带预约ID和病历ID）
    
private slots:
    void on_backButton_clicked();
    void on_createRecordButton_clicked();
    void on_createPrescriptionButton_clicked();
    void on_medicalRecordTable_cellDoubleClicked(int row, int column);
    void onMessageReceived(MessageType type, const QJsonObject& data);
    
private:
    void loadPatientInfo();
    void loadAppointmentInfo();
    void loadMedicalRecords();
    void checkAppointmentCompletion();  // 检查本次预约是否已完成病历和处方
    void updatePatientInfo(const Patient& patient);
    void updateAppointmentInfo(const QJsonObject& appointment);
    void updateMedicalRecordsTable(const QJsonArray& records);
    
    Ui::PatientDetailWidget *ui;
    Doctor m_doctor;
    int m_patientId;
    int m_appointmentId;
    Patient m_patient;
    QJsonObject m_appointmentInfo;
    QJsonArray m_medicalRecords;  // 保存病历记录数据
    
    // 记录本次就诊的病历和处方完成状态
    bool m_hasCreatedRecord;      // 是否已创建病历
    bool m_hasCreatedPrescription; // 是否已创建处方
};

#endif // PATIENTDETAILWIDGET_H

